package com.androidcourse.shounews.Adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.androidcourse.shounews.R;
import com.androidcourse.shounews.Toast.BaseToast;

import java.util.List;

public class NewsTopAdapter extends RecyclerView.Adapter<NewsTopAdapter.NewsTopViewHolder> {
    private List<String> nameList;
    private OnItemClickListener listener;
    private int selectedPosition=0;
    public NewsTopAdapter(){

    }
    public NewsTopAdapter(List<String> nameList,OnItemClickListener listener) {
        this.nameList = nameList;
        this.listener=listener;
    }
    public interface OnItemClickListener {
        void onTopItemClick(int position);
    }

    public class NewsTopViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView textView;
        public NewsTopViewHolder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.recycle_view_top_item_title);
            itemView.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            int pre=selectedPosition;
            selectedPosition=getAdapterPosition();
            //通知改变
            notifyItemChanged(selectedPosition);
            if(pre!=-1){
                notifyItemChanged(pre);
            }
            listener.onTopItemClick(getAdapterPosition());
        }
    }

    @NonNull
    @Override
    public NewsTopViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_view_top_item,parent,false);
        return new NewsTopViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NewsTopViewHolder holder, int position) {
        holder.textView.setText(nameList.get(position));
        if(selectedPosition==position){
            holder.textView.setTextSize(15);
            holder.textView.setTextColor(ContextCompat.getColor(holder.textView.getContext(), R.color.font_selected_color));
        }else {
            holder.textView.setTextSize(13);
            holder.textView.setTextColor(ContextCompat.getColor(holder.textView.getContext(), R.color.font_normal_color));
        }
    }

    @Override
    public int getItemCount() {
        return nameList.size();
    }
}
